/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.SelectionListener;
import com.sun.lwuit.layouts.BoxLayout;

/**
 *
 * @author Kvest
 */
public class DefaultTimeSelectForm extends TimeSelectForm implements ActionListener, SelectionListener
{
    private Command ok;
    private Command back;
    private FormAction listener;

    private ComboBox time;
    private Label tfLabel;
    private TextField yourTime;

    public DefaultTimeSelectForm(String title,String cbTitle, String backCommandName, FormAction listener)
    {
        super(title);
        this.listener = listener;

        init(cbTitle, backCommandName);
    }

    public String getTimeStr()
    {
        int selectedIndex = time.getSelectedIndex();
        if (ViewManager.getViewManager().getFunctionMenuType() != FunctionMenu.CHERNOVTSI_TYPE)
        {
            switch (selectedIndex)
            {
                case 0: return "5";
                case 1: return "10";
                case 2: return "15";
                case 3: return "20";
                case 4: return yourTime.getText();
                default: return "10";
            }
        }
        else
        {
            switch (selectedIndex)
            {
                case 0: return "2";
                case 1: return "5";
                case 2: return "7";
                case 3: return "10";
                case 4: return yourTime.getText();
                default: return "5";
            }
        }
    }

    public void reset()
    {
        time.setSelectedIndex(1);
        removeComponent(tfLabel);
        removeComponent(yourTime);
        yourTime.setText("");
    }

    public void selectionChanged(int oldSelected, int newSelected)
    {
        removeComponent(tfLabel);
        removeComponent(yourTime);
        if (newSelected == time.size() - 1)
        {
            addComponent(tfLabel);
            addComponent(yourTime);
        }
    }

    private void init(String cbTitle, String backCommandName)
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addCommandListener(this);

        back = new Command(backCommandName, BACK_ID);
        addCommand(back);

        ok = new Command("Ok", OK_ID);
        addCommand(ok);

        addComponent(new Label(cbTitle));
	time = new ComboBox();
        time.setSelectCommandText("");
        if (ViewManager.getViewManager().getFunctionMenuType() != FunctionMenu.CHERNOVTSI_TYPE)
        {
            time.addItem("5");
            time.addItem("10");
            time.addItem("15");
            time.addItem("20");
        }
        else
        {
            time.addItem("2");
            time.addItem("5");
            time.addItem("7");
            time.addItem("10");
        }
        time.addItem("Cвое время");
        time.addSelectionListener(this);
        addComponent(time);

        tfLabel = new Label("Ваше время");

        yourTime = new TextField();
        yourTime.setSelectCommandText("");
        yourTime.setInputModeOrder(new String[]{"123"});
        yourTime.setConstraint(TextArea.NUMERIC);
    }

    public void actionPerformed(ActionEvent evt)
    {
        if (listener != null)
        {
            listener.commandAction(evt.getCommand().getId());
        }
    }
}



